package com.jiangjiang.eshop.membership.updater;

import com.jiangjiang.eshop.membership.constant.MemberLevel;
import com.jiangjiang.eshop.membership.domain.MemberLevelDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberLevelEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointEntity;
import com.jiangjiang.eshop.membership.service.MemberLevelDetailService;
import com.jiangjiang.eshop.membership.service.MemberLevelService;
import com.jiangjiang.eshop.membership.service.MemberPointDetailService;
import com.jiangjiang.eshop.membership.service.MemberPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName PublishCommentMembershipUpdater
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/6 23:32
 * @Version 1.0
 **/
@Component
public class PublishCommentMembershipUpdater extends AbstractMembershipUpdater<Object, Boolean>{

    /**
     * 发表评论成长值
     */
    private static final Long PUBLISH_COMMENT_GROWTH_VALUE_UPDATE = 10L;
    /**
     * 晒单的成长值
     */
    private static final Long SHOW_PICTURES_GROWTH_VALUE_UPDATE = 10L;
    /**
     * 发表评论会员积分
     */
    private static final Long PUBLISH_COMMENT_MEMBER_POINT_UPDATE = 10L;
    /**
     * 晒单的会员积分
     */
    private static final Long SHOW_PICTURES_MEMBER_POINT_UPDATE = 10L;
    /**
     * 会员等级管理Service组件
     */
    @Autowired
    private MemberLevelService memberLevelService;
    /**
     * 会员积分管理Service组件
     */
    @Autowired
    private MemberPointService memberPointService;



    @Autowired
    public PublishCommentMembershipUpdater(MemberLevelDetailService memberLevelDetailService,
                                           MemberPointDetailService memberPointDetailService) {

        super(memberLevelDetailService, memberPointDetailService);
    }

    @Override
    protected MemberLevelDetailEntity updateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        Boolean isShowPicture = (Boolean) parameter;
        MemberLevelDetailEntity detail = new MemberLevelDetailEntity();
        MemberLevelEntity entity = memberLevelService.queryByAccountId(userAccountId);
        // 更新之前记录旧值
        detail.setOldGrowthValue(entity.getGrowthValue());
        detail.setOldMemberLevel(entity.getLevel());
        // 执行更新
        entity.setGrowthValue(entity.getGrowthValue() + PUBLISH_COMMENT_GROWTH_VALUE_UPDATE + (
                isShowPicture ? SHOW_PICTURES_GROWTH_VALUE_UPDATE : 0
        ));
        entity.setLevel(MemberLevel.get(entity.getGrowthValue()));
        memberLevelService.save(entity);
        // 记录更新后的新值
        detail.setNewGrowthValue(entity.getGrowthValue());
        detail.setNewMemberLevel(entity.getLevel());
        detail.setUpdatedGrowthValue(PUBLISH_COMMENT_GROWTH_VALUE_UPDATE + (
                isShowPicture ? SHOW_PICTURES_GROWTH_VALUE_UPDATE : 0
        ));
        return detail;
    }

    @Override
    protected MemberPointDetailEntity updateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        Boolean isShowPicture = (Boolean) parameter;
        MemberPointDetailEntity detail = new MemberPointDetailEntity();
        MemberPointEntity entity = memberPointService.queryByAccountId(userAccountId);
        // 更新之前记录旧值
        detail.setOldMemberPoint(entity.getPoint());
        // 执行更新
        entity.setPoint(entity.getPoint() + PUBLISH_COMMENT_MEMBER_POINT_UPDATE + (
                isShowPicture ? SHOW_PICTURES_MEMBER_POINT_UPDATE : 0
        ));
        memberPointService.save(entity);
        // 记录更新后的新值
        detail.setNewMemberPoint(entity.getPoint());
        detail.setUpdatedMemberPoint( PUBLISH_COMMENT_MEMBER_POINT_UPDATE + (
                isShowPicture ? SHOW_PICTURES_MEMBER_POINT_UPDATE : 0
        ));
        return detail;
    }

    @Override
    protected Boolean getExecuteResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }

    @Override
    protected String getMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return "发表了一条评论" + ((Boolean) parameter ? "，同时晒了单" : "");
    }

    @Override
    protected String getMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return "发表了一条评论" + ((Boolean) parameter ? "，同时晒了单" : "");
    }

    @Override
    protected MemberLevelDetailEntity undoUpdateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        Boolean isShowPicture = (Boolean) parameter;
        MemberLevelDetailEntity detail = new MemberLevelDetailEntity();
        MemberLevelEntity entity = memberLevelService.queryByAccountId(userAccountId);
        // 更新之前记录旧值
        detail.setOldGrowthValue(entity.getGrowthValue());
        detail.setOldMemberLevel(entity.getLevel());
        // 执行更新
        entity.setGrowthValue(entity.getGrowthValue() - PUBLISH_COMMENT_GROWTH_VALUE_UPDATE - (
                isShowPicture ? SHOW_PICTURES_GROWTH_VALUE_UPDATE : 0
        ));
        entity.setLevel(MemberLevel.get(entity.getGrowthValue()));
        memberLevelService.save(entity);
        // 记录更新后的新值
        detail.setNewGrowthValue(entity.getGrowthValue());
        detail.setNewMemberLevel(entity.getLevel());
        detail.setUpdatedGrowthValue(- PUBLISH_COMMENT_GROWTH_VALUE_UPDATE - (
                isShowPicture ? SHOW_PICTURES_GROWTH_VALUE_UPDATE : 0
        ));
        return detail;
    }

    @Override
    protected MemberPointDetailEntity undoUpdateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        Boolean isShowPicture = (Boolean) parameter;
        MemberPointDetailEntity detail = new MemberPointDetailEntity();
        MemberPointEntity entity = memberPointService.queryByAccountId(userAccountId);
        // 更新之前记录旧值
        detail.setOldMemberPoint(entity.getPoint());
        // 执行更新
        entity.setPoint(entity.getPoint() - PUBLISH_COMMENT_MEMBER_POINT_UPDATE - (
                isShowPicture ? SHOW_PICTURES_MEMBER_POINT_UPDATE : 0
        ));
        memberPointService.save(entity);
        // 记录更新后的新值
        detail.setNewMemberPoint(entity.getPoint());
        detail.setUpdatedMemberPoint(- PUBLISH_COMMENT_MEMBER_POINT_UPDATE - (
                isShowPicture ? SHOW_PICTURES_MEMBER_POINT_UPDATE : 0
        ));
        return detail;
    }

    @Override
    protected Boolean getUndoResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }

    @Override
    protected String getUndoMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return "发表的一条评论被删除了" + ((Boolean)parameter ? "，同时这个评论是晒单的" : "");
    }

    @Override
    protected String getUndoMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return "发表的一条评论被删除了" + ((Boolean)parameter ? "，同时这个评论是晒单的" : "");
    }
}
